﻿using System.Collections.Generic;
using UnityEngine;

// 大地图数据信息组件
public class MapEditorHelper : MonoBehaviour
{
    public int wSize;
    public int hSize;
    public int TILE_WIDTH;
    public int TILE_HEIGHT;
    public string savePath;
    public string tilesPath;
    public string mapName;
    public int mutil = 1;
    public int Lod = 1;
    public int Size = 1;
    /// <summary>
    /// 是否显示子项
    /// </summary>
    /// <param name="isShow"></param>
    public void ShowItem(bool isShow)
    {
        var childCount = this.transform.childCount;
        var hideflags = isShow ? HideFlags.None : HideFlags.HideInHierarchy;
        for (int i = 0; i < childCount; i++)
        {
            this.transform.GetChild(i).hideFlags = hideflags;
        }
    }

    // 检查所有的地图块 如果发现有相同位置的地图块 直接报错 阻止导出
    public bool CheckItem()
    {
        bool bCheck = true;
        Dictionary<string, GameObject> mapTiles = new Dictionary<string, GameObject>();
        var childCount = this.transform.childCount;
        for (int i = 0; i < childCount; i++)
        {
            Transform childT =  this.transform.GetChild(i);
            Vector3 pos = childT.transform.position;
            if (pos.x > WorldEntry.m_mapSize || pos.z > WorldEntry.m_mapSize)
            {
                Debug.LogError("存在越界的地图块 请选择删除 =========> " + pos.ToString(), childT.gameObject);
            }
            float int_x = pos.x / 15;
            float int_y = pos.z / 15;
            if (int_x != (int)int_x || int_y != (int)int_y)
            {
                Debug.LogError("存在中心点不对的地图块 请选择删除 =========> " + pos.ToString(), childT.gameObject);
            }
            string key = pos.x + "_" + pos.z;
            if (mapTiles.ContainsKey(key))
            {
                bCheck = false;
                GameObject obj = mapTiles[key];
                GameObject obj1 = childT.gameObject;
                Debug.LogError("存在重复的地图块 请选择删除 =========> " + pos.ToString(), obj);
                Debug.LogError("存在重复的地图块 请选择删除 =========> " + pos.ToString(), obj1);
            }
            else
            {
                mapTiles[key] = childT.gameObject;
            }
        }
        return bCheck;
    }

    // 对所有tile排序
    public void SortTile()
    {
        List<Transform> tiles = new List<Transform>();
        var childCount = transform.childCount;
        // 先捕获所有tile的Transform
        for (int i = 0; i < childCount; i++)
        {
            Transform childT = this.transform.GetChild(i);
            tiles.Add(childT);
        }
        //if (tiles.Count != wSize * hSize)
        //{
        //    Debug.LogError("地图块数量不正确 =========> tile num : " + tiles.Count);
        //}
        tiles.Sort(compareMapChuckPos);
        // 根据所有tile的pos 进行排序
        for (int i = tiles.Count - 1; i >= 0; i--)
        {
            Transform tile = tiles[i];
            Vector3 pos = tile.transform.position;
            tile.SetSiblingIndex(i);
            Debug.Log("设置地图块 =========> pos : " + pos.ToString() + " idx : " + i);
        }
    }

    public int compareMapChuckPos(Transform a, Transform b)
    {
        Vector3 posa = a.transform.position;
        int idxa = (int)(posa.x / TILE_WIDTH) + ((int)(posa.z / TILE_HEIGHT) * wSize);
        Vector3 posb = b.transform.position;
        int idxb = (int)(posb.x / TILE_WIDTH) + ((int)(posb.z / TILE_HEIGHT) * wSize);
        return (int)idxa - (int)idxb;
    }
}
